﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Viewer.BusinessObject.BusinessEntities;
using Ext.Net;

namespace Viewer.NhanSu.UserControl
{
    public partial class UCNghiPhep : System.Web.UI.UserControl
    {

        NSNghiPhep nghiPhep = new NSNghiPhep();
        private List<NSNghiPhep> lst;
        NSNhanVien nhanvien = new NSNhanVien();
        CommonCode common = new CommonCode();
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        private void ResetControl()
        {
            txtNoiDung.Text = string.Empty;
            txtLyDo.Text = string.Empty;
            dtNgayHieuLuc.Text = DateTime.Today.ToString("dd/MM/yyyy");
            dtNgayHetHieuLuc.Text = DateTime.Today.ToString("dd/MM/yyyy");
        }
        [DirectMethod]
        public void ShowDialog(string type, string iD, string nhanvienID, string typeParent)
        {

            hdNhanVienId.Text = nhanvienID;
            hdId.Text = iD;
            if (type.Equals("AddNew"))
            {
                wdNghiPhep.Title = "Tạo mới nghỉ phép";
                ResetControl();
                hdType.Text = type;
                btnSaveNghiPhep.Hidden = false;
                hdParentType.Text = typeParent;
            }
            else if (type.Equals("Edit"))
            {
                wdNghiPhep.Title = "Sửa đổi nghỉ phép";
                hdType.Text = type;
                btnSaveNghiPhep.Hidden = false;
                LoadDataNBL(Convert.ToInt16(iD));
            }
            else if (type.Equals("View"))
            {
                wdNghiPhep.Title = "Xem chi tiết nghỉ phép";
                hdType.Text = type;
                btnSaveNghiPhep.Hidden = true;
                LoadDataNBL(Convert.ToInt16(iD));
            }
            wdNghiPhep.Show();
        }
        public void LoadDataNBL(int id)
        {
            lst = (List<NSNghiPhep>)Session["NghiPhep"];
            NSNghiPhep item = lst.Where(a => a.NghiPhepID.Equals(id)).SingleOrDefault();
            dtNgayHieuLuc.Text = item.ThoiGianBatDau!=null? ((DateTime)item.ThoiGianBatDau).ToString("dd/MM/yyyy") : string.Empty;
            dtNgayHetHieuLuc.Text = item.ThoiGianKetThuc != null? ((DateTime)item.ThoiGianKetThuc).ToString("dd/MM/yyyy"): string.Empty;
            txtLyDo.Text = item.LyDoNghiPhep;
            txtNoiDung.Text = item.GhiChu;


        }
        protected void btnSaveNghiPhep_CLick(object sender, DirectEventArgs e)
        {
            DateTime ngayBatDau = Convert.ToDateTime(dtNgayHieuLuc.Text);
            DateTime ngayKetThuc = Convert.ToDateTime(dtNgayHetHieuLuc.Text);

            int nhanVienId = !string.IsNullOrEmpty(hdNhanVienId.Text) ? Convert.ToInt16(hdNhanVienId.Text) : 0;

            if (hdType.Text.Equals("AddNew"))
            {
                NSNghiPhep item = nghiPhep.GetNghiPhep(nhanVienId, ngayBatDau, ngayKetThuc, txtNoiDung.Text, txtLyDo.Text);
                ((UCNhanVien)this.Parent).GetNghiPhep(item);
            }
            else if (hdType.Text.Equals("Edit"))
            {
                nghiPhep.EditNghiPhep(Convert.ToInt16(hdId.Text), ngayBatDau, ngayKetThuc, txtNoiDung.Text, txtLyDo.Text);
                ((UCNhanVien)this.Parent).ReloadGrid("NghiPhep");
            }

            wdNghiPhep.Hide();

        }
    }
}